/**
 *
 */
package com.tc.dispatch.web.schedule.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.tc.dispatch.gsalgorithm.spec.parse;
import com.tc.dispatch.gsalgorithm.spec.spec;
import com.tc.dispatch.gsalgorithm.spec.specConst;
import com.tc.dispatch.gsservice.basic.common.regulation;
import com.tc.dispatch.gsservice.ejb.GSServiceLocal;
import com.tc.dispatch.gsservice.exception.GSException;
import com.tc.dispatch.gsservice.model.GSConstants;
import com.tc.dispatch.gsservice.model.OtherConstants;
import com.tc.dispatch.gsservice.model.SsignDensity;
import com.tc.dispatch.web.gsservice.gsHelper;
import com.tc.dispatch.web.util.DateJsonValueProcessor;
import com.tc.dispatch.web.util.FloatJsonValueProcessor;

/**
 * @author cajeep
 *
 */
public class otherConstants extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public otherConstants() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	public Float getOtherConstants(String name,String type)
	{
		GSServiceLocal gss = gsHelper.getGSServiceLocal();

		Float ret = new Float(0);
		String whereStr="";
		try {
			if(type.equals(""))
				whereStr=" where name='"+name+"'";
			else if(type.equals(specConst.YuanZhu)||type.equals(specConst.TaoKong)||type.equals(specConst.PaoDao)||type.equals(specConst.TuoYuan)||type.equals(specConst.PaoDaoKong))
				{
					
					whereStr=" where name='"+name+"' and specType='"+specConst.YuanZhu+"'";
				}
			else if(type.indexOf("ssign")>=0)
				{
				String ssign=type.split("_")[1];
				List sdList=gss.findSsignDensity(" where ssign='"+ssign+"'");
				int high=0;
				if(sdList.size()>0)
					high=((SsignDensity)sdList.get(0)).getHigh();
				if(high==0)
					whereStr=" where name='低"+name+"'";
				else
					whereStr=" where name='高"+name+"'";
				}
			else if(type.indexOf("电镀")>=0)
			{
				String[] arr=type.split("_");
				String plate=arr[1];
				String ssign=arr[2];
				System.out.println(type);
				List sdList=gss.findSsignDensity(" where ssign='"+ssign+"'");
				String nameStr="";
				int high=0;
				if(sdList.size()>0)
					high=((SsignDensity)sdList.get(0)).getHigh();
				if(high==0)
					name="低";
				else
					name="高";
				plate=plate.toLowerCase();
				if(plate.indexOf("zn")>-1||plate.indexOf("锌")>-1)
				{
					name+="ZN";
				}
				else
				{
					name+="NI";
				}
				name+="电镀默认成品率";
				whereStr=" where name='"+name+"'";
			}
			else
				whereStr=" where name='"+name+"' and specType='"+specConst.Fangkuai+"'";
			System.out.println("otherConstants:"+whereStr);
			OtherConstants oc=(OtherConstants)gss.findOtherConstants(whereStr).get(0);
			ret =oc.getConstant();
		} catch (GSException gse) {
			System.out.println("getOtherConstants error :" + gse.getMessage());
			return ret;
		}
		catch(Exception e)
		{

			System.out.println("Exception error :" + e.getMessage());
			return ret;
		}
		return ret;

	}

	public Float getQianDaoFee(String spec,Float grossWeight)
	{
		GSServiceLocal gss = gsHelper.getGSServiceLocal();

		Float ret = new Float(0);
		try {
			spec m_spec=parse.parseSpec(spec);
			if(m_spec.sort().equals(specConst.YuanZhu))
			{
				ret=new Float(regulation.getQianDao(m_spec));
			}
			else
			{
				ret=new Float(regulation.getQianDaoFk(grossWeight.floatValue()));
			}
		} catch (Exception e) {
			System.out.println("getQianDaoFee error :" + e.getMessage());
			return ret;
		}
		return ret;
	}

	public Float getBaoZhuangFei(String spec,String ssign)
	{
		GSServiceLocal gss = gsHelper.getGSServiceLocal();

		Float ret = new Float(0);
		try {
			OtherConstants oc=(OtherConstants)gss.findOtherConstants(" where name='包装单价'").get(0);
			ret =oc.getConstant();
			spec m_spec=parse.parseSpec(spec);
			List sdList=gss.findSsignDensity(" where ssign='"+ssign+"'");
			float density=0;
			if(sdList.size()>0)
				density=((SsignDensity)sdList.get(0)).getDensity().floatValue();
			float net=m_spec.volume()*density/1000000;
			System.out.println(net+":"+ret.floatValue());
			ret=new Float(net*ret.floatValue());
		} catch (GSException gse) {
			System.out.println("getPeiFang error :" + gse.getMessage());
			return ret;
		}
		return ret;

	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 *
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		// response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		if (action == null)
			action = "";

		GSServiceLocal gss = gsHelper.getGSServiceLocal();
		HttpSession sess = request.getSession();
		if(action.equals("list"))
		{
			 String page=request.getParameter("start");
		        if(page==null)
		          page="0";
		        String pagePerNum=request.getParameter("limit");
		        if(pagePerNum==null)
		          pagePerNum="10";
		        try{

		        	List ret=gss.findOtherConstantsByPage(Integer.parseInt(page),Integer.parseInt(pagePerNum),"");

		        	JSONObject json = new JSONObject();

					JsonConfig cfg = new JsonConfig();
					cfg.registerJsonValueProcessor(java.util.Date.class,
							new DateJsonValueProcessor());
					cfg.registerJsonValueProcessor(java.sql.Date.class,
							new DateJsonValueProcessor());
					cfg.registerJsonValueProcessor(java.lang.Float.class, new FloatJsonValueProcessor());


					// BeanUtils.copyProperties();
					json.put("totalProperty", new Integer(gss.getOtherCosntantsTotalNum("")));

					JSONArray arr = JSONArray.fromObject(ret.toArray(), cfg);
					json.put("root", arr);

					response.getWriter().print(json);
		        }
		        catch(GSException e)
		        {

		        	System.out.println("list error:"+e.getMessage());
		        	response.getWriter().print("{success:false}");
		        }

		}

		else if(action.equals("update"))
		{
			String id=request.getParameter("id");
			String name=request.getParameter("name");
			String constant=request.getParameter("constant");
			String specType=request.getParameter("specType");
			String bz=request.getParameter("bz");
			try{

				OtherConstants oc=new OtherConstants(Integer.parseInt(id),name,Float.valueOf(constant),specType,bz,new Date());
				gss.updateOtherConstants(oc);
			}
			catch(GSException e)
			{
				System.out.println("update error:"+e.getMessage());
			}
			catch(Exception e)
			{
				System.out.println("exception:"+e.getMessage());
			}
			out.print("{success:true, data:'sucess'}");
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
